<?php
namespace App\Services;

use App\Models\VersionModel;
use Framework\BaseService;

class AppService extends BaseService
{

    public function getVersionInfo(int $type, string $version)
    {
//最新版本
        $newInfo = VersionModel::query()
            ->where('type', $type)
            ->where('status', 1)
            ->orderByDesc('id')
            ->limit(1)
            ->first();
        if ($newInfo == null) {
            $this->retError("请在后台添加版本信息");
        }
        //前端需要获取的版本
        $getInfoExist = VersionModel::query()
            ->where('type', $type)
            ->where('status', 1)
            ->where('version', $version)
            ->exists();

        $vInfo = [
            'version' => $newInfo->version,
            'update_content' => $newInfo->content,
            'is_update' => 1,
        ];
        if ($getInfoExist) {
            $isUpdate = 0;
            if ($newInfo->version != $version) {
                $version = $newInfo->version;
                $isUpdate = 1;
            }
            $vInfo = [
                'version' => $version,
                'update_content' => $newInfo->content,
                'is_update' => $isUpdate,
            ];
        }
        if ($type == 1) {
            $vInfo['android_url'] = $newInfo->url;
            $vInfo['android_update'] = $vInfo['is_update'];
            $vInfo['android_version'] = $vInfo['version'];
        } else {
            $vInfo['ios_url'] = $newInfo->url;
            $vInfo['ios_update'] = $vInfo['is_update'];
            $vInfo['ios_version'] = $vInfo['version'];
        }
        $vInfo['is_apk'] = $newInfo->is_apk; // 是否是apk地址
        $vInfo['is_force'] = $newInfo->is_force; // 是否是是否其强制
        unset($vInfo['is_update'], $vInfo['version']);

        return $vInfo;
    }
}
